/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.realriu.snipzel.model;

import com.uwyn.jhighlight.renderer.XhtmlRenderer;
import com.uwyn.jhighlight.renderer.XhtmlRendererFactory;
import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author riu
 */
public class CodeSnippet {

    public static final String UNKNOWN_LANGUAGE_STRING = "UNKNOWN";
    private long id;
    private String title;
    private String code;
    private String language;
    private String tags;

    public CodeSnippet(long id, String title, String language, String code, String tags) {
        this.id = id;
        this.title = title;
        this.code = code;
        this.tags = tags;

        if(XhtmlRendererFactory.getSupportedTypes().contains(language)){
            this.language = language;
        }else{
            this.language = UNKNOWN_LANGUAGE_STRING;
        }
    }

    public CodeSnippet(long id, String title, String language, String tags) {
        this(id, title, language, null, tags);
    }

    public String getCode() {
        return code;
    }

    public long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public String getHTMLCode(){
        if(code != null){
            String html = "";

            
            XhtmlRenderer r = (XhtmlRenderer) XhtmlRendererFactory.getRenderer(XhtmlRendererFactory.JAVA);
            try {
                
                html = r.highlight(title, code, "utf-8", false);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            return html;
        }

        return null;
    }

    public String getLanguage() {
        return language;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    @Override
    public boolean equals(Object o) {
        if(o!=null && o instanceof CodeSnippet && ((CodeSnippet)o).getId()==id){
            return true;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return new Long(id).hashCode();
    }

    

    
}
